const {
  MemberModel
} = require('../../../utils/member-model.js');
var wxcode = require('../../../utils/index.js');
var memberModel = new MemberModel();
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    phoneShow: false,
    encryptedDataUser: '',
    ivUser: '',
    qrCode: "",
    remainTime: 0, //倒计时初始值
    timerName: "", //计时器名字
    canvasHidden: false,
    maskHidden: true,
    imagePath: '',
    placeholder: '', //默认二维码生成文本
    timer: '', //定时器名字
    formId: ''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  formSubmit(e) {
    memberModel.userMessage({
      merchantId: wx.getStorageSync('merchantId'),
      formIds: [e.detail.formId]
    }).then(() => {
      this.getUserCode()
    }).catch(() => {
      this.getUserCode()
    })
  },
  formSubmit1(e) {
    this.setData({
      formId: e.detail.formId
    })
  },
  /**
   * 调接口动态获取qrcode编码
   */
  getUserCode: function () {
    wx.reportAnalytics('qrcode_refresh');
    clearInterval(this.data.timer);
    memberModel.userCode({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      var that = this;
      that.setData({
        qrCode: data.code.replace(/\s/g, '').replace(/(\d{3})(?=\d)/g, "$1 "), //二维码
        remainTime: data.refreshTime, //倒计时时间
        placeholder: data.code
      }, function () {
        that.createQrCode(); //创建二维码
        that.startReportHeart();
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 设置二维码
   */
  createQrCode: function () {
    var that = this;
    wxcode.barcode('barcodeCanvas', that.data.placeholder, 420, 96)
    wxcode.qrcode(that.data.placeholder, "myQrcode", 310, 310)
  },

  /**
   * 计时器
   */
  startReportHeart: function () {
    clearInterval(this.data.timer);
    var that = this;
    let countDownNum = that.data.remainTime; //获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          remainTime: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          that.getUserCode();
        }
      }, 1000)
    })
  },

  getUserInfo({
    detail
  }) {
    const {
      encryptedData,
      iv
    } = detail
    if (encryptedData) {
      this.setData({
        encryptedDataUser: encryptedData,
        ivUser: iv,
        phoneShow: true
      })
    }
  },
  getOrderInfo() {
    if (this.data.formId) {
      memberModel.userMessage({
        merchantId: wx.getStorageSync('merchantId'),
        formIds: [this.data.formId]
      })
    }
    this.getUserCode();
    this.setData({
      isLogin: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogin: app.globalData.isLogin
    })
    if (app.globalData.isLogin) {
      this.getUserCode();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})